/*******************************************************************************
* File Name: main.c
*
* Version: 1.00
*
* Description:
*  This is the source code for the Unique ID example project of the cy_boot
*  component.
*
********************************************************************************
* Copyright 2015, Cypress Semiconductor Corporation. All rights reserved.
* This software is owned by Cypress Semiconductor Corporation and is protected
* by and subject to worldwide patent and copyright laws and treaties.
* Therefore, you may use this software only as provided in the license agreement
* accompanying the software package from which you obtained this software.
* CYPRESS AND ITS SUPPLIERS MAKE NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* WITH REGARD TO THIS SOFTWARE, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT,
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*******************************************************************************/

#include <project.h>
#include "debug.h"
#include "hal_temp_hum.h"
#include <stdio.h>
#include "gizwits_product.h"
#include "common.h"

#define DIE_MASK            (0xFFu)

#if(CY_PSOC4)
    #define DIE0_LOT0       (0u)
    #define DIE0_LOT1       (8u)
    #define DIE0_LOT2       (16u)
    #define DIE1_SORT       (16u)
    #define DIE1_MINOR      (24u)
#else
    #define DIE0_LOT_LSB    (0u)
    #define DIE0_LOT_MSB    (8u)
    #define DIE0_REV_ID     (16u)
    #define DIE1_WRK_WK     (16u)
    #define DIE1_FAB_YR     (24u)
#endif  /* (CY_PSOC4) */

#define DIE0_WAFER          (24u)
#define DIE1_X              (0u)
#define DIE1_Y              (8u)

/* Pointer to a two element 32-bit unsigned integer array */
uint32 uniqueId[2];

/*******************************************************************************
* Function Name: Timer_Interrupt
********************************************************************************
*
* Summary:
*  Handles the Interrupt Service Routine for the WDT timer.
*
*******************************************************************************/
CY_ISR(Timer_Interrupt)
{
    int i;
    
    if(CySysWdtGetInterruptSource() & CY_SYS_WDT_COUNTER0_INT)
    {
        TIMER_IRQ_FUN();
            
        /* Clears interrupt request  */
        CySysWdtClearInterrupt(CY_SYS_WDT_COUNTER0_INT);
    }
    
    WDT_Interrupt_ClearPending();
}

/*******************************************************************************
* Function Name: WDT_Start
********************************************************************************
*
* Summary:
*  Configures WDT to trigger an interrupt with period 100ms for keyboard
*  activity simulation.
*
*******************************************************************************/

void WDT_Start(void)
{
    /* Unlock the WDT registers for modification */
    CySysWdtUnlock(); 
    
    /* Setup ISR */
    WDT_Interrupt_StartEx(&Timer_Interrupt);
    /* Write the mode to generate interrupt on match */
    CySysWdtWriteMode(CY_SYS_WDT_COUNTER0, CY_SYS_WDT_MODE_INT);
    /* Configure the WDT counter clear on a match setting */
    CySysWdtWriteClearOnMatch(CY_SYS_WDT_COUNTER0, true);
    /* Configure the WDT counter match comparison value */
    CySysWdtWriteMatch(CY_SYS_WDT_COUNTER0, 32767/1000);
    /* Reset WDT counter */
    CySysWdtResetCounters(CY_SYS_WDT_COUNTER0);
    
    /* Enable the specified WDT counter */
    CySysWdtEnable(CY_SYS_WDT_COUNTER0_MASK);

    /* Lock out configuration changes to the Watchdog timer registers */
    CySysWdtLock();    
    
    
}


/*******************************************************************************
* Function Name: WDT_Stop
********************************************************************************
*
* Summary:
*  This API stops the WDT timer.
*
*******************************************************************************/
void WDT_Stop(void)
{
    /* Unlock the WDT registers for modification */
    CySysWdtUnlock(); 
    /* Disable the specified WDT counter */
    CySysWdtDisable(CY_SYS_WDT_COUNTER0_MASK);
    /* Locks out configuration changes to the Watchdog timer registers */
    CySysWdtLock();    
}

void ManageSystemPower(void)
{
    uint8 interruptStatus;
        
    interruptStatus  = CyEnterCriticalSection();

    //CySysPmDeepSleep();
    CySysPmSleep();

    CyExitCriticalSection(interruptStatus); 
    
    return;
}

void Handle_Sensor(void)
{
    uint8 temp,hum;
    static uint32 timer = 0;
    uint32 current_time = gizGetTimerCount();
    
    if(current_time-timer > 5000)
    {
        timer = current_time;
        DHT11_Read_Data(&temp, &hum);
        printf("temp:%dC hum:%d%%RH\r\n",temp,hum);
        
        currentDataPoint.valuehum_int = hum;
        currentDataPoint.valuetmp_int = temp;
    }
    
    return;
}

CY_ISR(HandleUart2)
{
    uint8 tmp[33]={0};
    int i=0;
 
    int mask = UART_2_GetRxInterruptSourceMasked();
        
    while(UART_2_SpiUartGetRxBufferSize()&& i<32)
    {
        tmp[i] = UART_2_SpiUartReadRxData();
        i++;
    }
    
    if(i)
    {
        gizPutData(tmp, i);
        //printf("[%d] %s\n",i,tmp);
    }
    
    UART_2_ClearRxInterruptSource(mask);
}

CY_ISR(key1_isr_handler)
{
	CyDelay(10);
    if(Pin_key1_Read() == 0)
	{
        gizwitsSetMode(WIFI_AIRLINK_MODE);
    }
    
    Pin_key1_ClearInterrupt();
    key1_isr_ClearPending();
    
}

/*******************************************************************************
* Function Name: main
********************************************************************************
*
* Summary:
*  At the main function, gets unique id of the device and prints it to UART.
*
* Parameters:
*  None.
*
* Return:
*  None.
*
*******************************************************************************/
int main()
{
    /* An array of char elements for the resulting string to be stored */
    char commonUniqueIdInfo[60u];
    char uniqueUniqueIdInfo[80u];

    CyGlobalIntEnable;
    
    WDT_Start();
    UART_1_Start();
    key1_isr_StartEx(key1_isr_handler);
    
    /* Gets unique id of the device */
    CyGetUniqueId(uniqueId);

    CyDelay(1000);
	printf("DHT11 Init %d\r\n",DHT11_Init());
    
    userInit();
    gizwitsInit();
    
    UART_2_Start();
    UART_2_SetCustomInterruptHandler(HandleUart2);
    
    while(1)
    {
        Handle_Sensor();
        userHandle();
        gizwitsHandle((dataPoint_t *)&currentDataPoint);
        
        ManageSystemPower();
    }
}

/* [] END OF FILE */
